<template>
  <div class="main_sty">
    <div class="top">
      <el-cascader v-model="category" ref="myCascader" :options="data2"
                   :props="{value:'pcId',children:'children', label: 'pcName',expandTrigger: 'hover', checkStrictly: true }"
                   @change="handleChange"  clearable></el-cascader>
      &nbsp;&nbsp;  | &nbsp;&nbsp;
      搜索：<el-input placeholder="汽车型号" suffix-icon="el-icon-search" v-model="input" style="width: 160px"  @change="selectInfo"></el-input>
      <el-button  type="primary" style="margin: 12px;padding:9px">
        <router-link :to="{path:'/addProduct'}">
          <i class="el-icon-circle-plus">新增产品</i>
        </router-link>
      </el-button>

    </div>
    <div class="separator_sty"></div>
    <el-tabs v-model="activeName"  @tab-click="handleClick" style="padding-left: 10px">
      <el-tab-pane :label="`产品列表`" name="first" >
        <div class="tab">
          <el-table
            ref="multipleTable"
            :data="productList"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="handleSelectionChange">

            <el-table-column
              prop="zproductByProductId.productName"
              label="产品名称"
              width="200">
            </el-table-column>

            <el-table-column label="图片" align="center"   width="120px">
              <template slot-scope="scope">
                <el-image
                  class="table-td-thumb"
                  :src="scope.row.zproductByProductId.prImgpath"
                  :preview-src-list="[scope.row.zproductByProductId.prImgpath]"
                ></el-image>
              </template>
            </el-table-column>
            <el-table-column
              prop="zproductByProductId.prModel"
              label="型号"
              width="200">
            </el-table-column>
            <el-table-column
              prop="psPrice"
              label="售价"
              width="120">
            </el-table-column>
            <el-table-column
              prop="psNumber"
              label="库存数量"
              width="120px"
              show-overflow-tooltip>
            </el-table-column>
            <el-table-column
              prop="psUnit"
              label="计数量"
              width="120px"
              show-overflow-tooltip>
            </el-table-column>
            <el-table-column
              label="入库时间"
              width="200">
              <template slot-scope="scope">{{ scope.row.psDate |formatDate }}</template>
            </el-table-column>
            <el-table-column
              prop="operation"
              label="操作"
              show-overflow-tooltip>
              <template slot-scope="scope" >
                <a href="" title="编辑">
                  <i class="el-icon-edit"></i>
                </a>
                <a  title="视图">
                  <i class="el-icon-tickets" @click="proudctMain(scope.row.psId)"></i>
                </a>
              </template>
            </el-table-column>
          </el-table>
        </div>


        <div class="page">
          <el-pagination style="float:right;margin-top: 5px;" :current-page.sync="currentPage2" :page-sizes="[10, 25, 40,100]"
                         :page-size="pageSize2" layout="total, sizes, prev, pager, next, jumper"
                         :total="productList.length"
                         @current-change="changeProSize"
                         @size-change="handleSizeChange2">
          </el-pagination>
        </div>
      </el-tab-pane>
    </el-tabs>

    <el-drawer
      title="我是标题"
      :visible.sync="drawer"
      :direction="direction"
      :with-header="false"
      size="50%">

      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-row>
              <el-col :span="24">
                <div style="border: 1px solid red ; width: 100%;height: 100px; background-color: #20a0ff"></div>
              </el-col>
            </el-row>
          </div>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
  import {formatTimeToStr} from '../assets/format'

  export default {
    data() {
      return {
        currentPage2:0,
        pageSize2:10,
        category:'',//类型查询
        data2: [],//产品分类数组
        searchPro:'',//添加商品里面的查询
        pcId:0,//类型id
        pbId:0,//品牌id
        ///////////////
        drawer:false,
        direction:'rtl',
        value:'',//汽车品牌搜索
        options:[],//汽车品牌
        productList:[],//产品信息
        input:'',//汽车型号搜索
        colData: [{title: "名称",istrue: true},//动态列
          {title: "型号",istrue: true},],
        colOptions: [], //默认全选
        colSelect: [],
        activeName: 'first',//卡片名称

        //分页
        totalSize:0,
        pageSize:0,
        currentPage:1,
        sizes:[10, 15, 20],//分页可改变的页数
      };
    },
    created :function() {
      //加载产品分类
      this.$axios.get("http://localhost:8088/buInfo/category")
        .then(v=>{
          this.data2=v.data;
        }).catch();

      this.initProduct(this.searchPro,this.pcId,this.pbId);

      var _this = this;
      for (let i = 0; i < _this.colData.length; i++) {
        _this.colSelect.push(_this.colData[i].title);
        /*if (_this.colData[i].title == '名称') { //初始化不想展示的列可以放在这个条件里
          continue;
        }*/
        _this.colOptions.push(_this.colData[i].title);

      }

    },
    methods: {
      //前端分页
      changeProSize: function (size) {
        this.currentPage2 = size;
        console.log("yy"+this.pageSize)  //每页下拉显示数据
      },
      handleSizeChange2: function(currentPage){
        this.pageSize2 = currentPage;
        console.log("kk"+this.pageNum)  //点击第几页
      },
      proudctMain(id){
        this.$router.push({path:'saproductmain', query: {key:id}})
      },
      //类型加品牌查询 和模糊查询
      handleChange(item) {
        if(this.category !='' ){
          this.pbId=item[0];
          this.pcId=item[1];

        }else{
          this.pbId=0;
          this.pcId=0;
        }
        this.initProduct(this.searchPro,this.pcId,this.pbId);

      },
      //产品列表
      initProduct(searchPro,pcId,pbId){
        this.$axios.get("http://localhost:8088/buInfo/newproduct_info",{
          params:{
            productName:searchPro,
            pcId:pcId,
            pbId:pbId
          }
        }).then(v=>{
          this.productList=v.data;
        }).catch();
      },

      /////6_8久代码

      selectInfo(){
        if(this.value==null||this.input==null){
          this.initData(this.currentPage,this.pageSize,this.value,this.input);
        }else {
          this.initData(this.currentPage,this.pageSize,this.value,this.input);
        }
      },
      opl(){
        this.$message("okok")
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
      },
      changePage(){},
      handleSizeChange(){},










      goBack(){
        this.$router.go(-1);
      },
      handleClick(tab, event) {
        console.log(tab, event);
      }
    },
    watch: {
      colOptions(valArr) {
        var arr = this.colSelect.filter(i => valArr.indexOf(i) < 0); // 未选中
        this.colData.filter(i => {
          if (arr.indexOf(i.title) != -1) {
            i.istrue = false;
            this.$nextTick(() => {
              this.$refs.multipleTable.doLayout();
            });
          } else {
            i.istrue = true;
            this.$nextTick(() => {
              this.$refs.multipleTable.doLayout();
            });
          }
        });
      }
    },
    filters: {
      formatDate: function (time) {
        if (time != null && time != "") {
          var date = new Date(time);
          return formatTimeToStr(date, "yyyy-MM-dd hh:mm:ss");
        } else {
          return "";
        }
      }
    }
  }
</script>


<style scoped>
  .tab{

    width: 100%;
    max-height: 370px;
    overflow-y: auto;
  }
  .grid-content {
    border-radius: 4px;
    height: 700px;
  }
  .bg-purple {
    background: #F4F8FB;
  }
  .main_sty{
    border:1px solid #ccc;
    width:100%;
    height: 535px;
    background: white;
    border-radius: 5px;
    box-shadow:#ccc 0px 0px 10px;
    overflow:hidden;
  }
  .table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
  }
  .page{
    float: right;
    padding: 15px 0 20px 0;
  }
  .separator_sty{
    border:1px solid #409eff;
    margin: 0 auto;
  }
  .top{
    padding-left: 10px;
  }
</style>
